﻿@using ORM.Business.Models.Entities
@model ORM.Business.Models.Entities.Answer
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/ckeditor/adapters/jquery.js")"></script>
@Html.TextAreaFor(item => item.ContentText, new { id = "answerContent" })
@Html.ValidationMessageFor(model => model.ContentText)
<br />
<div style="float: right">
    <input type="button" id="submitAnswer" value="Gửi Câu Trả Lời" style="height: 25px;
        width: 120px; font-size: 13px" /></div>
<script type="text/javascript">
    var answerEditor =  CKEDITOR.replace('answerContent',
    {
    filebrowserImageBrowseUrl: '/Scripts/ckfinder/ckfinder.html?type=Images',
    filebrowserImageUploadUrl: '/Scripts/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images',
    removePlugins : 'elementspath',
    });
    $(document).ready(function() {
        //var isPosting = false;
        $("#submitAnswer").click(function () {
            $(document.createElement('div'))
        .attr({title: 'Fixtech.vn', 'class': 'alert'})
        .html("Bạn muốn gửi câu trả lời ngay bây giờ?")
        .dialog({
            buttons: {"Xác Nhận": function() {
                            for (var instanceName in CKEDITOR.instances) {
            var string = CKEDITOR.instances[instanceName].getData();
            string = string.replace(/<\/?[^>]+(>|$)/g, "");
            string = string.replace(/[&]nbsp[;]/gi," ");
            if(string.trim() != "") {
                CKEDITOR.instances[instanceName].updateElement();
            }            
        }
            $('#answerContent').attr("disable","disable");
            var content = $('#answerContent').val();
                $.ajax ({
                    type: "POST",
                    url: "@Url.Action("SubmitAnswer", "Question")",
                    dataType: "text",
                    data: {  
                        QuestionId: @Model.QuestionId,
                        ContentHtml: content
                    },
                    success: function (data) {
                        if(data=="true") {
                            answerEditor.setData("");
                            alert("Câu trả lời của bạn đã được gửi để chờ duyệt!");
 //                       $('#answerListField').append(data);
//                        var firstli = $('.comment-list li').first();
//                        $(firstli).show(200, function(){$(this).fadeTo(300,1)});

//                        if(data != "") {
//                            var total = parseInt($("#totalAnswer").html());
//                            $("#totalAnswer").html(total + 1);
//                        }
                        } else {
                            alert(data);
                        }
                          
                    },
                    error: function (result,error) {
                        alert ( " Can't do because: " + error.responseText );                        
                    },

                    complete: function() {
//                        isPosting = false;
                        $('#answerContent').attr("disable","");
                    }
                });
                $(this).dialog('close');
            }, "Huỷ": function(){$(this).dialog('close');}},
            close: function(){$(this).remove();},
            draggable: true,
            modal: true,
            resizable: false,
            width: 500,
            open: function(event, ui) {
        $("button").blur();
    }
});


        });
    });
    
</script>
